<?php
header("Content-type:text/html; charset=UTF-8");
if (isset($_POST['uid']) && !empty($_POST['ph'])){
  if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
     $openid=addslashes(trim($_POST['uid']));
     $phone=addslashes(trim($_POST['ph']));
     $code=addslashes(trim($_POST['code']));

     $yanChi=array(
       1 => 2448, 
       2 => 3779,
       3 => 5833,
       4 => 7241,
       5 => 6352,
       6 => 4473,
       7 => 3544,
       8 => 2635,
       9 => 1726,
       10 => 3817,
       11 => 4128,
       12 => 5239,
       13 => 6361,
       14 => 7479,
       15 => 3884,
       16 => 5995,
       17 => 6717,
       18 => 7829,
       19 => 9938,
       20 => 4962,
      ); 

     if (empty($openid)) {
       echo 2;
       eixt;
     }
     if (empty($phone)) {
       echo 2;
       eixt;
     }
     class SmsApi {
  const apiurl='https://api.4321.sh/sms/send';
  const apikey='N505020898';
  const secret='50502f7930d5dc3c';
  public function sendsms($mobile,$content,$sign_id) {  
    $postData = array (
      'apikey'  =>  self::apikey,
      'secret' => self::secret,
      'content' => $content,
      'mobile' => $mobile,
      'sign_id' => $sign_id
        );
    $result = $this->curlPost( self::apiurl ,$postData);
     
    return $result;
  }
  
  private function curlPost($url,$postFields){
    $postFields = json_encode($postFields);
    $ch = curl_init ();
    curl_setopt( $ch,CURLOPT_URL, $url); 
    curl_setopt( $ch,CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json; charset=utf-8'
      )
    );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,1); 
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
    $ret = curl_exec ( $ch );
    curl_close ( $ch );
    return $ret;
  }
  
}
if (isset($yanChi[$code])) {
    $txt=$yanChi[$code];
}else{
exit;
}
 
$SmsApi=new SmsApi();
$getS=$SmsApi->sendsms($phone,'您的验证码为'.$txt.'，如非本人操作请忽略本短信',135808);

include '../c/config.php';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);

 $mysqli->query("INSERT INTO `sms_log` (`id`, `uid`, `phone`,`msg`,`createtime`) VALUES (NULL,'".$openid."','".$phone."','".$getS."','".date("Y-m-d H:i:s")."')");
    

echo '{"s":1}';
 

  }
}
?>